package cn.com.zpark.controller;

import cn.com.zpark.pojo.Emp;
import cn.com.zpark.service.ILoginService;
import cn.com.zpark.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @PackageName: cn.com.zpark.controller
 * @ClassName: LoginServletController
 * @Description:
 * @author: RZS
 * @date: 2021/12/8  11:01
 */
@WebServlet("/login.do")
public class LoginServletController extends HttpServlet {

    private ILoginService loginService = new LoginServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 1、获取用户的请求参数
         * 2、调用service处理用户登录
         * 3、调用view展示结果
         */
        // 1、获取用户的请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 创建session对象
        HttpSession session = request.getSession();

        // 2、调用service处理用户登录
        Emp emp = loginService.login(username, password);
        // 判断emp对象是否为null
        if(emp == null){
            // 登录失败
            session.setAttribute("msg", "用户名或者密码错误");
            response.sendRedirect(request.getContextPath()+"/view/login.jsp");
        }else{
            response.getWriter().write(emp.toString());
        }


    }
}
